package cn.tedu.boot34.controller;

import cn.tedu.boot34.entity.User;
import cn.tedu.boot34.mapper.UserMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wxd
 * @version 1.0
 */
@RestController
public class UserController {
    final
    UserMapper mapper;

    public UserController(UserMapper mapper) {
        this.mapper = mapper;
    }

    @RequestMapping("/reg")
    public int reg(@RequestBody User user) {
        User u = mapper.selectByUsername(user.getUsername());
        if ("".equals(user.getUsername()) || "".equals(user.getPassword())) {
            return 3;
        }
        if (u == null) {
            mapper.insert(user);
            return 1;
        }
        return 2;
    }
    @RequestMapping("/login")
    public int login(@RequestBody User user) {
        User u = mapper.selectByUsername(user.getUsername());
        if (u != null) {
            if (u.getPassword().equals(user.getPassword())) {
                return 1;
            }
            return 2;
        }
        return 3;
    }
}
